package com.qkl.xiatang.app.service.feed;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.dto.BusinessDTO;
import com.qkl.xiatang.app.dto.PageOutDTO;
import com.qkl.xiatang.app.dto.feed.DecrQtyDTO;
import com.qkl.xiatang.app.dto.feed.WeighGroupQtyDTO;
import com.qkl.xiatang.app.dto.feed.WeighGroupQtyDetailDTO;
import com.qkl.xiatang.app.dto.feed.WeighGroupQtyInDTO;
import com.qkl.xiatang.app.dto.message.FeedMsgDTO;
import com.qkl.xiatang.app.entity.feed.FeedAllocDetailEntity;
import com.qkl.xiatang.app.entity.feed.FeedAllocQtyEntity;
import com.qkl.xiatang.app.entity.pond.PondEntity;
import com.qkl.xiatang.app.entity.user.AppUserInfo;
import com.qkl.xiatang.app.mapper.FeedAllocMapper;
import com.qkl.xiatang.app.mapper.FeedAllocQtyMapper;
import com.qkl.xiatang.app.mapper.FeedWeighMapper;
import com.qkl.xiatang.app.service.impl.FeedStockService;
import com.qkl.xiatang.app.service.impl.MessageService;
import com.qkl.xiatang.app.service.impl.PrintService;
import com.qkl.xiatang.app.utils.AppSessionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class FeedWeighService {

    @Autowired
    private FeedWeighMapper feedWeighMapper;

    @Autowired
    private FeedAllocMapper feedAllocMapper;

    @Autowired
    private PrintService printService;

    @Autowired
    private MessageService messageService;

    @Autowired
    private FeedAllocQtyMapper feedAllocQtyMapper;

    @Autowired
    private FeedStockService feedStockService;


    /**
     * 查询称饲料任务按重量分组
     * @param dto
     * @return
     */
    public BusinessDTO<PageOutDTO<WeighGroupQtyDTO>> queryWeighGroupQty(WeighGroupQtyInDTO dto){
        List<String> pondIdList = dto.getPondIdList();
        List<String> groupIdList = dto.getGroupIdList();
        dto.setCompanyId(AppSessionUtils.getCompanyId());
        dto.checkParam();

        Integer totalRow = feedWeighMapper.queryWeighGroupQtyTotal(dto);
        totalRow = null == totalRow? 0:totalRow;
        PageOutDTO<WeighGroupQtyDTO> outDTO = new PageOutDTO();
        outDTO.settingPage(dto,totalRow);
        if(totalRow > 0){
            outDTO.setList(feedWeighMapper.queryWeighGroupQty(dto));
        }
        if(ObjectUtils.isEmpty(outDTO.getList())){
            outDTO.setList(new ArrayList());
        }
        return BusinessDTO.sucessBusiness(outDTO,"查询称饲料任务按重量分组");
    }

    /**
     * 查询称饲料任务按重量分组-明细 暂不使用
     * @param dto
     * @return
     */
    @Deprecated
    public BusinessDTO<PageOutDTO<WeighGroupQtyDetailDTO>> queryWeighGroupQtyDetail(WeighGroupQtyInDTO dto){
        dto.setCompanyId(AppSessionUtils.getCompanyId());
        dto.checkParam();
        dto.toTaskIdList();
        if(ObjectUtils.isEmpty(dto.getTaskIdList())){
            return BusinessDTO.failBusiness("参数不能为空");
        }

        Integer totalRow = feedWeighMapper.queryWeighGroupQtyDetailTotal(dto);
        totalRow = null == totalRow? 0:totalRow;
        PageOutDTO<WeighGroupQtyDetailDTO> outDTO = new PageOutDTO();
        outDTO.settingPage(dto,totalRow);
        if(totalRow > 0){
            outDTO.setList(feedWeighMapper.queryWeighGroupQtyDetail(dto));
        }
        if(ObjectUtils.isEmpty(outDTO.getList())){
            outDTO.setList(new ArrayList());
        }
        return BusinessDTO.sucessBusiness(outDTO,"查询称饲料任务按重量分组-明细 ");
    }

    /**
     * 按重量完成称饲料任务
     * @param dto
     * @return
     */
    public BusinessDTO<Object> finishWeighGroupQty(WeighGroupQtyInDTO dto){
        String companyId = AppSessionUtils.getCompanyId();
        dto.setCompanyId(companyId);
        dto.setUserId(AppSessionUtils.getUserId());
        dto.setUserName(AppSessionUtils.getUserName());
        dto.toTaskIdList();
        if(ObjectUtils.isEmpty(dto.getTaskIdList())){
            return BusinessDTO.failBusiness("参数不能为空");
        }
        int count = feedWeighMapper.finishWeighGroupQty(dto);
        if(count < 1){
            return BusinessDTO.failBusiness("称饲料任务已完成,匆重复提交");
        }

        QueryWrapper queryWrapper = new QueryWrapper<FeedAllocDetailEntity>()
                                    .eq("company_id",companyId)
                                    .in("id",dto.getTaskIdList())
                                    .orderByAsc("alloc_date");
        printService.printWeighFeed(feedAllocMapper.selectList(queryWrapper));

        queryWrapper.groupBy("company_id","alloc_date","feed_id","serial_no","name");
        queryWrapper.select("company_id","alloc_date","feed_id","serial_no","name");
        messageService.sendFeedMessageByWeigh(feedAllocMapper.selectList(queryWrapper));
        return BusinessDTO.sucessBusiness(new HashMap<>(),"按重量完成称饲料任务");
    }

    /**
     * 按重量取消称饲料任务
     * @param dto
     * @return
     */
    @Transactional
    public BusinessDTO<Object> cancelWeighGroupQty(WeighGroupQtyInDTO dto){
        dto.toTaskIdList();
        if(ObjectUtils.isEmpty(dto.getTaskIdList())){
            return BusinessDTO.failBusiness("参数不能为空");
        }
        List<String> taskIdList = dto.getTaskIdList();
        List<FeedMsgDTO>  feedMsgList = feedWeighMapper.queryFeedMessage(taskIdList.get(0));
        if(ObjectUtils.isEmpty(feedMsgList)){
            return BusinessDTO.failBusiness("称饲料任务已取消");
        }
        AppUserInfo userInfo = AppSessionUtils.getSessionUser();
        String companyId = AppSessionUtils.getCompanyId();
        String userId = AppSessionUtils.getUserId();

        List<PondEntity>  pondMsgList = feedWeighMapper.queryPondMessage(taskIdList);
        List<DecrQtyDTO> decrQtyDTOS = new ArrayList<>();
        if(ObjectUtils.isNotEmpty(feedMsgList)){
            for (FeedMsgDTO msgDTO : feedMsgList){
                DecrQtyDTO qtyDTO = new DecrQtyDTO();
                qtyDTO.setQty(msgDTO.getQty());
                qtyDTO.setFeedId(msgDTO.getFeedId());
                decrQtyDTOS.add(qtyDTO);
            }
        }

        QueryWrapper queryWrapper = new QueryWrapper<FeedAllocDetailEntity>()
                .eq("company_id",companyId)
                .eq("status",0)
                .in("id",taskIdList);
        queryWrapper.groupBy("company_id","alloc_date","serial_no","name");
        queryWrapper.select("company_id","alloc_date","serial_no","name");
        queryWrapper.orderByAsc("alloc_date");
        List<FeedAllocDetailEntity> taskList = feedAllocMapper.selectList(queryWrapper);

        feedAllocQtyMapper.delete(new QueryWrapper<FeedAllocQtyEntity>().in("detail_id",taskIdList));
        feedAllocMapper.delete(new QueryWrapper<FeedAllocDetailEntity>().in("id",taskIdList).eq("status",0));
        feedStockService.feedStock(userId,true,decrQtyDTOS);
        messageService.sendCancelFeedListMessage(userInfo,taskList,pondMsgList,feedMsgList);
        return BusinessDTO.sucessBusiness(new HashMap<>(),"按重量取消称饲料任务");
    }

    /**
     * 查询称饲料餐次
     * @return
     */
    public BusinessDTO<Object> queryWeighMeal(){
        List<Map<String,Object>> list = feedWeighMapper.queryWeighMeal(AppSessionUtils.getCompanyId());
        if(ObjectUtils.isEmpty(list)){
            list = new ArrayList<>();
        }
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        return BusinessDTO.sucessBusiness(map,"查询称饲料餐次");
    }

}
